package com.jswlw.controller.api.wrapper;

import com.jswlw.controller.api.resp.DevTypeResponse;
import com.jswlw.entity.biz.DevType;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.service.biz.DevTypeService;
import com.jswlw.service.biz.DistributorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Component
public class DevTypeWrapper {
    @Autowired
    DevTypeService devTypeService;

    @Autowired
    DistributorService distributorService;

    public DevTypeResponse devTypeList(Integer distributorId) {

        DevTypeResponse response = new DevTypeResponse();
        Distributor distributor = distributorService.find4Id(distributorId);
        if (distributor.getLevel() == 3) {
            distributor = distributorService.find4Id(distributor.getParentId());
        }

        List<DevType> list = distributor.getDevTypeList();
        List<DevTypeResponse.DevTypeData> data = new ArrayList<>();
        list.forEach(devType -> {
            data.add(DevTypeResponse.DevTypeData
                    .builder()
                    .value(String.valueOf(devType.getId()))
                    .name(devType.getName())
                    .parent(0)
                    .build());
        });

        response.setData(data);
        response.success();
        return response;
    }
}
